<template>
  <view class="form-container">
    <uni-forms ref="form" :modelValue="formState" :rules="rules" labelPosition="top">
      <view class="background-white bor-top">
        <uni-forms-item name="handleContent" :label="label" :label-width="100" required>
          <textarea v-model="formState.handleContent" class="text-area" placeholder="请输入" :maxlength="1000" />
        </uni-forms-item>
      </view>
      <view class="background-white bor-top bottom" v-if="meetingNodeType != 8">
        <uni-forms-item name="files" label="图片（最多三张）：" labelWidth="150">
          <image-upload state="edit" :imageUrl="formState.files" @change="imageOnChange"> </image-upload>
        </uni-forms-item>
      </view>
    </uni-forms>
    <view class="submit" @click="handleSubmit">{{ meetingNodeType != 8 ? '汇报' : '确认' }}</view>
  </view>
</template>

<script>
import CuUserSelector from '@/components/CuUserSelector/index.vue'
import CuCellGroup from '@/components/cu-cell/cu-cell-group.vue'
import CuCellBackNodesSelect from '@/components/CuCellBackNodesSelect/index.vue'
import ImageUpload from '@/components/image-upload/index.vue'

import { scHandle, sbHandle, ahHandle, confirmPass } from '@/api/duty-basic/duty-meeting'

const requestMap = {
  2: scHandle,
  3: sbHandle,
  4: ahHandle,
  4: ahHandle,
  8: confirmPass
}
const titleMap = {
  2: '生产主办汇报',
  3: '设备主办汇报',
  4: '安全主办汇报',
  8: '班组确认会议内容'
}

export default {
  name: '',
  data() {
    return {
      meetingNodeType: -1,
      meetingId: '',
      procTaskId: '',
      formState: {
        handleContent: '', // 工作内容
        files: ''
      },
      rules: {
        handleContent: {
          rules: [
            {
              required: true,
              errorMessage: '工作要求不能为空'
            }
          ]
        }
      }
    }
  },
  components: { CuUserSelector, CuCellGroup, CuCellBackNodesSelect, ImageUpload },
  methods: {
    imageOnChange(urlList) {
      this.formState.files = urlList.join(',')
    },
    async handleSubmit() {
      await this.$refs.form.validate()

      let request = requestMap[this.meetingNodeType]
      if (!request) return

      //入参处理
      const params = {
        procTaskId: this.procTaskId,
        meetingId: this.meetingId,
        ...this.formState
      }

      if (this.meetingNodeType == '8') {
        params.advice = params.handleContent
        delete params.handleContent
      }
      uni.showModal({
        content: '提交后，填写的内容将不可以修改，请确定填写无误后再提交',
        confirmText: '确定',
        success: async (res) => {
          if (res.confirm) {
            await request(params)
            uni.showToast({
              title: '提交成功！',
              icon: 'success',
              duration: 2000,
              success() {
                uni.navigateBack()
              }
            })
          }
        }
      })
    }
  },
  computed: {
    label() {
      return this.meetingNodeType == 8 ? '当班情况：' : '工作要求：'
    }
  },
  onReady() {
    // 需要在onReady中设置规则
    this.$refs.form.setRules(this.rules)
  },
  onLoad({ meetingNodeType, procTaskId, id }) {
    uni.setNavigationBarTitle({
      title: titleMap[meetingNodeType]
    })
    this.meetingNodeType = meetingNodeType
    this.meetingId = id
    this.procTaskId = procTaskId
  }
}
</script>

<style lang="scss" scoped>
.text-area {
  height: 300rpx;
}

.submit {
  width: 600rpx;
  height: 88rpx;
  background: #454a5e;
  border-radius: 50rpx 50rpx 50rpx 50rpx;
  opacity: 1;
  text-align: center;
  line-height: 88rpx;
  font-size: 36rpx;
  font-weight: 400;
  color: #ffffff;
  margin: 0 auto;
  margin-top: 70rpx;
  margin-bottom: 70rpx;
}

.form-item-label-position-left {
  flex-direction: row !important;
  background: #fff;
  padding: 0 30rpx;
  padding-right: 20rpx;
  position: relative;
  box-sizing: border-box;
  min-height: 100rpx;
  align-items: center;
  margin-bottom: 0;

  & ::v-deep .uni-forms-item__label {
    height: auto;
    padding-bottom: 0rpx !important;
  }

  & ::v-deep .uni-data-checklist,
  & ::v-deep .uni-forms-item__content {
    position: initial;
  }

  & ::v-deep .uni-forms-item__error {
    left: 30rpx;
    top: 62%;
  }
}

.form-item-label-position-left::before {
  content: '';
  height: 1rpx;
  width: calc(100% - 30rpx);
  bottom: 0;
  left: 30rpx;
  position: absolute;
  background: #d5d7e3;
}

.checkbox {
  padding-left: 30rpx;
}
</style>
